package com.yidu.view;

import com.yidu.dao.BookUserMapper;
import com.yidu.domain.BookUser;
import com.yidu.service.BookUserService;
import com.yidu.utils.BaseMessage;
import com.yidu.utils.Tools;
import com.yidu.view.vo.BookUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>@Description: 用户Controller</p>
 * <p>@Author yidu-liandyao</p>
 * <p>@Date: 2021-01-11</p>
 * <p>@Time: 14:00</p>
 */
@Controller
@RequestMapping("/user") //这个类的前缀是/user
public class BookUserController {

    @Resource
    BookUserService service;


    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String,Object> findAll(BookUserVo vo){
        //查询数据
        List<BookUser> list = service.findAll(vo);
        //查询行数
        int count = service.findCount(vo);
        return Tools.getLayUiMap(list,count) ;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public BaseMessage delete(Integer id){
        int rows =service.delete(id);
        BaseMessage bm = new BaseMessage();
        bm.setState(rows);
        return bm ;
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequestMapping("/deleteAll")
    @ResponseBody
    public BaseMessage deleteAll(String ids){

        int rows =service.deleteAll(ids);
        BaseMessage bm = new BaseMessage();
        bm.setMsg("共删除了"+rows+"行记录");
        bm.setState(rows);
        return bm ;
    }

    /**
     * 增加或者修改
     * @param user
     * @return
     */
    @RequestMapping("/addOrUpdate")
    @ResponseBody
    public BaseMessage addOrUpdate(
            @RequestParam(value = "userPhoto_") MultipartFile file,
            BookUserVo user,
            HttpServletRequest request,
            HttpSession session){
        //获取上传的文件名
        String fileName = file.getOriginalFilename();
        //在springMVC中如何取到request,response,session
        int index = fileName.lastIndexOf('.');
        String hz = fileName.substring(index);

        //使用jdk提供的随机字符串
        String newFileName = UUID.randomUUID().toString() + hz;
        //真实路径
        String realPath = request.getServletContext().getRealPath("/upload");
        File target = new File(realPath+"/"+newFileName);
        try {
            //保存文件
            file.transferTo(target);
        } catch (IOException e) {
            e.printStackTrace();
        }
        user.setUserPhoto("/upload/"+newFileName);
        //增加
        service.insertSelective(user);

        BaseMessage bm = new BaseMessage();
        bm.setMsg("增加成功!");
        bm.setState(1);
        return bm ;
    }

    /*
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),
                true));
    }
    */
}
